package com.meida.paotui.bus.provider.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.model.SaveModel;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.utils.ApiAssert;
import com.meida.common.utils.FlymeUtils;
import com.meida.common.utils.StringUtils;
import com.meida.module.user.client.entity.AppRole;
import com.meida.module.user.client.entity.AppUser;
import com.meida.module.user.provider.mapper.AppUserMapper;
import com.meida.module.user.provider.service.AppAccountService;
import com.meida.module.user.provider.service.AppRoleService;
import com.meida.paotui.bus.client.entity.BusIdentity;
import com.meida.paotui.bus.provider.service.AccountService;
import com.meida.paotui.bus.provider.service.BusIdentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zyf
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AccountServiceImpl extends BaseServiceImpl<AppUserMapper, AppUser> implements AccountService {

    @Autowired
    private AppRoleService roleService;

    @Autowired
    private AppAccountService appAccountService;
    @Autowired
    private BusIdentityService busidentityService;

    /**
     * 注册手机账号
     */
    @Override
    public ResultBody registerAccount(SaveModel model, String accountName, String telephone, String areaCode, String smsCode, String userType, String password, String userName, String nickName, String cardNo, String cardNoImg, String cardNoImg2) {

        CriteriaSave cs = new CriteriaSave(model, AppUser.class);
        cs.put("accountName", accountName);
        cs.put("telephone", telephone);
        cs.put("smsCode", smsCode);
        cs.put("password", password);
        cs.put("userType", userType);
        cs.put("userName", userName);
        if (FlymeUtils.isEmpty(nickName)) {
            nickName = "mulu" + RandomUtil.randomInt(10000, 50000);
        }
        cs.put("nickName", nickName);
        ApiAssert.anyOneIsNotNull("请至少填写一个账户", telephone, accountName);
        AppRole role = roleService.getByRoleCode(userType);
        if (FlymeUtils.isNotEmpty(role)) {
            cs.put("roleId", role.getRoleId());
        }
        if (FlymeUtils.isNotEmpty(telephone)) {
            cs.put("mobile", telephone);
            if (FlymeUtils.isEmpty(accountName)) {
                //当accountName为空时用telephone当注册账户
                accountName = telephone;
            }
        }

        ResultBody resultBody = baseAdd(cs);
        //用户保存成功后注册账户
        if (resultBody.isOk()) {
            AppUser appUser = (AppUser) resultBody.getData();
            //骑手端注册特殊处理
            if (FlymeUtils.isNotEmpty(cardNo) && FlymeUtils.isNotEmpty(cardNoImg) && FlymeUtils.isNotEmpty(cardNoImg2)) {
                BusIdentity identity = new BusIdentity();
                identity.setCardNo(cardNo);
                identity.setCardNoImg(cardNoImg);
                identity.setCardNoImg2(cardNoImg2);
                identity.setIsPass(2);
                identity.setUserId(appUser.getUserId());
                busidentityService.save(identity);
            }
            //注册账户信息
            regAccount(accountName, areaCode, smsCode, password, resultBody, appUser);
            resultBody.setMsg("注册成功");
        }
        return resultBody;
    }

    private void regAccount(String accountName, String areaCode, String smsCode, String password, ResultBody resultBody, AppUser appUser) {
        if (FlymeUtils.isNotEmpty(accountName)) {
            if (StringUtils.matchEmail(accountName)) {
                //注册邮箱账户
                appAccountService.registerEmailAccount(appUser.getUserId(), accountName, password);
            } else if (StringUtils.isPhoneLegal(accountName, areaCode)) {
                //校验验证码
                redisUtils.validSmsCode(accountName, smsCode);
                //注册手机号账户
                appAccountService.registerMobileAccount(appUser.getUserId(), accountName, password, true);
            } else {
                //注册用户名账户
                appAccountService.registerUsernameAccount(appUser.getUserId(), accountName, password, true);
            }
        }
        resultBody.msg("注册成功");
    }

    @Override
    public ResultBody getAccountStatus(String userId) {
        CriteriaQuery<EntityMap> cq = new CriteriaQuery(AppUser.class);
        cq.select(AppUser.class, "*");
        cq.eq(AppUser.class, "userId");
        return basePageList(cq);
    }

    /**
     * 获取用户详细信息，审核状态
     *
     * @param userId
     * @return
     */
    @Override
    public ResultBody getuserInfo(String userId) {
        CriteriaQuery<EntityMap> cq = new CriteriaQuery(AppUser.class);
        cq.select(AppUser.class, "*");
        cq.select(BusIdentity.class, "userId", "cardNo", "cardNoImg", "cardNoImg2", "isPass");
        cq.eq(AppUser.class, "userId");
        cq.createJoin(BusIdentity.class);
        return basePageList(cq);
    }

    @Override
    public ResultBody getIdentity(String userId, ResultBody resultBody) {
        BusIdentity identity = busidentityService.getById(userId);
        resultBody.put("identity", identity);
        return resultBody;
    }

    @Override
    public ResultBody saveIdentity(long userId, String userName, String cardNo, String cardNoImg, String cardNoImg2,ResultBody resultBody) {
        BusIdentity identity =new BusIdentity();
        if(FlymeUtils.isNotEmpty(userName)){
            identity.setUserName(userName);
        }else{
            resultBody.setMsg("姓名不能为空");
            return resultBody;
        }
        if(FlymeUtils.isNotEmpty(userId)){
            identity.setUserId(userId);
        }else{
            resultBody.setMsg("userId不能为空");
            return resultBody;
        }
        if(FlymeUtils.isNotEmpty(cardNo)){
            identity.setCardNo(cardNo);
        }else{
            resultBody.setMsg("身份证号不能为空");
            return resultBody;
        }
        if(FlymeUtils.isNotEmpty(cardNoImg)){
            identity.setCardNoImg(cardNoImg);
        }else{
            resultBody.setMsg(" 请上传身份证正面");
            return resultBody;
        }
        if(FlymeUtils.isNotEmpty(cardNoImg2)){
            identity.setCardNoImg2(cardNoImg2);
        }else{
            resultBody.setMsg(" 请上传身份证反面");
            return resultBody;
        }
        identity.setIsPass(1);
        boolean a=  busidentityService.save(identity);
        if(a){
            resultBody.setMsg("提交成功");
            return resultBody;
        }
        return resultBody;
    }
}
